package stud.mpgame.network;

import stud.mpgame.network.UserObject;
import flash.utils.TypedDictionary;
import stud.mpgame.network.util.ResponseMessageDescription;

/**
 * ...
 * @author Timofeev
 */

/*abstract*/ class MessageDescription {    
    
    private static var registeredDescription:IntHash<MessageDescription> = new IntHash<MessageDescription>();
    private var registeredUserObjectIdentifies:TypedDictionary<Int, Class<Dynamic>>;
    private var registeredUserObjectClasses:TypedDictionary < Class<Dynamic>, Int > ;
    private var autoResponseFlag:Bool;
    
    
    private function new() {
        registeredUserObjectIdentifies = new TypedDictionary<Int,Class<Dynamic>>();
        registeredUserObjectClasses = new TypedDictionary<Class<Dynamic>,Int>();
    }
    
    public static function getMessageDescriptionByID(id:Int):MessageDescription {        
        return registeredDescription.get(id);
    }

    public function getIdentifiers():TypedDictionary<Int,Class<Dynamic>>{
        return registeredUserObjectIdentifies;
    }

    public static function registerDescription(messageDescription:MessageDescription):Void {
        registeredDescription.set(messageDescription.getID(), messageDescription);
    }

    public static function unRegisterDescription(messageDescription:MessageDescription):Void {
        registeredDescription.remove(messageDescription.getID());
    }
    
    private function registerUserObject(userObjectId:Int, userObjectClass:Class<Dynamic>):Void {        
        registeredUserObjectIdentifies.set(userObjectId, userObjectClass);
        registeredUserObjectClasses.set(userObjectClass, userObjectId);        
    }
    
    private function unRegisterUserObject(userObjectId:Int, userObjectClass:Class<Dynamic>):Void {
        registeredUserObjectIdentifies.remove(userObjectId);        
        registeredUserObjectClasses.delete(userObjectClass);        
    }
    
    public function createUserObject(id:Int):UserObject {
        var userObjectClass:Class<Dynamic> = registeredUserObjectIdentifies.get(id);
        return cast(Type.createInstance(userObjectClass, []), UserObject);        
    }

    /*abstract*/ public function getID():Int {
        throw "Abstract method";
        return -1;        
    }
    
    public function getUserObjectId(userObjectClass:Class<Dynamic>):Int {
		var check:Int = registeredUserObjectClasses.get(UserObject);
        return registeredUserObjectClasses.get(userObjectClass);
    }
    
    public function setAutoResponseFlag(autoResponseFlag:Bool):Void {
        this.autoResponseFlag = autoResponseFlag;
    }
    
    public function getAutoResponseFlag():Bool {
        return autoResponseFlag;
    }
}